#include "logging.h"

#include <string>
#include <fstream>

#include "macros.h"



Logging::Logging(LoggingType _type, int _rank)
{
	mRank = _rank;
	if(_type == RankLevel)
	{
		char rankIndexStr[256];
		memset(rankIndexStr, '\0', 256);
		itoa(mRank, rankIndexStr, 10);
		string tmpStr(rankIndexStr);
		string outputFileName = "Log_rank_" + tmpStr + ".txt";
		mFout = new ofstream(outputFileName);
	}
	else if(_type == MachineLevel)
	{
		mFout = new ofstream("Log.txt");
	}
	mLoggingType = _type;
}

Logging::~Logging()
{
	SAFE_DELETE(mFout);
}

void Logging::Output(const char *_info, bool _output2File)
{
	if(_output2File)
	{
		if(mLoggingType == MachineLevel)
			(*mFout)<<"Rank:"<<mRank<<" "<<_info<<std::endl;
		else if(mLoggingType == RankLevel)
			(*mFout)<<_info<<std::endl;
	}
	else
		cout<<"Rank:"<<mRank<<" "<<_info<<std::endl;
}